<?php
class ViewOnceUrlGen_Util_Email {
	
	public function sendNewAccountEmail($account)
	{
		$userToAddress = $account['email_address'];
		$subject = 'New Account Information';
		
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		
		$msg = file_get_contents('templates/user_added_tmplt.html');
		$msg = str_replace('{{username}}', $account['username'], $msg);
		$msg = str_replace('{{password}}', $account['password'], $msg);
		$msg = str_replace('{{role}}', $account['role'], $msg);
		$msg = str_replace('{{baseUrl}}', ViewOnceUrlGen_Util_Url::_siteURL().$baseUrl, $msg);
		
		$adminEmail = $this->_getAdminEmail();
		
		$mail = new Zend_Mail();
		$mail->setFrom($adminEmail);
		$mail->addTo($userToAddress);
		$mail->setSubject($subject);
		$mail->setBodyHtml($msg);
		$mail->send();
	}
	
	public function sendUpdatedAccountEmail($account)
	{
		$userToAddress = $account['email_address'];
		$subject = 'Account Profile Update Information';
		
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		
		$msg = file_get_contents('templates/user_updated_tmplt.html');
		$msg = str_replace('{{email_address}}', $account['email_address'], $msg);
		$msg = str_replace('{{first_name}}', $account['first_name'], $msg);
		$msg = str_replace('{{last_name}}', $account['last_name'], $msg);
		$msg = str_replace('{{role}}', $account['role'], $msg);
		$msg = str_replace('{{baseUrl}}', ViewOnceUrlGen_Util_Url::_siteURL().$baseUrl, $msg);
		
		$adminEmail = $this->_getAdminEmail();
		
		$mail = new Zend_Mail();
		$mail->setFrom($adminEmail);
		$mail->addTo($userToAddress);
		$mail->setSubject($subject);
		$mail->setBodyHtml($msg);
		$mail->send();
	}
	
	public function sendEmailChangedNotification($oldEmail, $newEmail)
	{
		$subject = 'Email Address Change Confirmation';
		
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		
		$msg = file_get_contents('templates/user_email_changed_tmplt.html');
		$msg = str_replace('{{old_email}}', $oldEmail, $msg);
		$msg = str_replace('{{new_email}}', $newEmail, $msg);
		$msg = str_replace('{{baseUrl}}', ViewOnceUrlGen_Util_Url::_siteURL().$baseUrl, $msg);
		
		$adminEmail = $this->_getAdminEmail();
		
		$mail = new Zend_Mail();
		$mail->setFrom($adminEmail);
		$mail->addTo(array($oldEmail, $newEmail));
		$mail->setSubject($subject);
		$mail->setBodyHtml($msg);
		$mail->send();
	}
	
	public function sendPasswordChangedNotification($account, $newPass)
	{
		$userToAddress = $account['email_address'];
		$subject = 'Password Change Confirmation';
		
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		
		$msg = file_get_contents('templates/user_password_changed_tmplt.html');
		$msg = str_replace('{{username}}', $account['username'], $msg);
		$msg = str_replace('{{password}}', $newPass, $msg);
		$msg = str_replace('{{baseUrl}}', ViewOnceUrlGen_Util_Url::_siteURL().$baseUrl, $msg);
		
		$adminEmail = $this->_getAdminEmail();
		
		$mail = new Zend_Mail();
		$mail->setFrom($adminEmail);
		$mail->addTo($userToAddress);
		$mail->setSubject($subject);
		$mail->setBodyHtml($msg);
		$mail->send();
	}
	
	
	public function sendLinkEmail($link)
	{
		$userToAddress = $link['send_to_email'];
		$adminEmail = $this->_getAdminEmail();
		$currentUserEmail = $this->_getCurrentUserEmail();
		$subject = 'Link to Website Order Form';
		
		$msg = file_get_contents('templates/send_link_tmplt.html');
		$msg = str_replace('{{generatedUrl}}', $link['generated_url'], $msg);
		$msg = str_replace('{{orderFormAccessPage}}', $this->_getOrderFormAccessPage(), $msg);
		$msg = str_replace('{{secureTokenId}}', $link['token'], $msg);
		
		$mail = new Zend_Mail();
		$mail->setFrom($adminEmail);
		$mail->addTo($userToAddress);
		$mail->addCc($adminEmail);
		if (isset($currentUserEmail)) {
			$mail->addCc($currentUserEmail);
		}
		$mail->setSubject($subject);
		$mail->setBodyHtml($msg);
		$mail->send();
	}
	
	protected function _getAdminEmail()
	{
		$config = Zend_Registry::get('config');
		return $config->admin->email;
	}
	
	protected function _getCurrentUserEmail()
    {
    	$auth = Zend_Auth::getInstance();
    	if ($auth->hasIdentity()) {
    		return $auth->getIdentity()->email_address;
    	} else {
    		return null;
    	}
    }
    
    protected function _getOrderFormAccessPage()
    {
    	$config = Zend_Registry::get('config');
    	$domain = ViewOnceUrlGen_Util_Url::_siteURL();
    	$orderFormAccessPage = $config->url->order->form->access;
    	return $domain . $orderFormAccessPage;
    }
}